package styledaco.data;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;

import sun.awt.image.ToolkitImage;

import com.oreilly.servlet.MultipartRequest;
import com.sun.imageio.plugins.png.PNGImageReader;

import comm.util.SoptFileRenamePolicy;
import comm.util.SoptHashMap;

public class DataProc {
	/*
	 * jpg,gif (MIME이 image만 받자-)
	 */
	public String fileUpload(HttpServletRequest request,String savePath) throws UnsupportedEncodingException{
        String uploadedFileName = "";
        String dataSeq = "";
        String articleSeq = "";
        SoptHashMap hm = new SoptHashMap();
        savePath = request.getRealPath(savePath);
        File f = new File(savePath);
        f.mkdir();
        
        int sizeLimit = 10 * 1024 * 1024 ; // 1메가까지 제한 넘어서면 예외발생
        SoptFileRenamePolicy renamePolicy = new SoptFileRenamePolicy();
                MultipartRequest multi = null;
				try {
					multi = new MultipartRequest(request, savePath, sizeLimit,"UTF-8", renamePolicy );
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                Enumeration formFileNames=multi.getFileNames();  // 폼의 이름 반환
                Enumeration formNames = multi.getParameterNames();
                
                String formFileName = (String)formFileNames.nextElement(); // 자료가 많을 경우엔 while 문을 사용
                String fileName = multi.getFilesystemName(formFileName); // 파일의 이름 얻기
                String formName = null;
                while(formNames.hasMoreElements()){
                                formName = (String) formNames.nextElement();
                                
                                hm.put(formName.toUpperCase(), multi.getParameter(formName).toString());
                }

            if(fileName == null) { // 파일이 업로드 되지 않았을때
            	uploadedFileName = "FAIL";
            } else { // 파일이 업로드 되었을때
                int dot = fileName.lastIndexOf(".");
                String ext = "";
                    if (dot != -1) {
                        ext = fileName.substring(dot+1);  // without "."
                        ext = ext.toLowerCase();
                    }
                    hm.put("ORIGIN_FILE", renamePolicy.originName);
                    File f2 = new File(savePath+"/"+fileName);
                    String mimeType = new MimetypesFileTypeMap().getContentType(f2);
                    
                    if(ext.equals("jpg")||ext.equals("jpeg")||ext.equals("gif")){
                    	uploadedFileName=new String(fileName);
                    	if(mimeType.indexOf("image")==-1){
                    		uploadedFileName="NOT_IMAGE";
                    		f2.delete();
                    	}else{
                    		//Success
                    	}
                    }else{
                    	uploadedFileName="NOT_IMAGE";
                    	f2.delete();
                    }
                
            } // end if
         
        return uploadedFileName;
    }
	
}